/*
 * ArticleCommentPresenter.java
 * business
 *
 * Created by chentao on 2017/4/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.comment;

import com.yidiandao.business.data.model.IArticleModel;
import com.yidiandao.business.data.recyclertype.ArticleComment;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;
import com.yidiandao.business.utils.NumberFormatUtils;

import java.util.List;

/**
 * Presenter
 */
public class ArticleCommentPresenter implements ArticleCommentContract.Presenter {

    private ArticleCommentContract.View mCommentView;

    private IArticleModel mArticleModel;

    private String mArticleId;

    public ArticleCommentPresenter(ArticleCommentContract.View view, String articleId, IArticleModel articleModel) {
        mCommentView = view;
        mArticleModel = articleModel;
        mArticleId = articleId;

        mCommentView.setPresenter(this);
    }

    @Override
    public void start() {
        loadComments(true);
    }

    @Override
    public void loadComments(final boolean isRefreshing) {
        mArticleModel.loadComments(mCommentView.getPagerTag(), isRefreshing, mArticleId, new IArticleModel.OnLoadResultListener<List<ArticleComment>>() {
            @Override
            public void onSuccess(List<ArticleComment> articleComments, boolean emptyData, boolean hasMore) {
                if (isRefreshing) {
                    mCommentView.refreshContent(mCommentView.checkResultObject(articleComments));
                    mCommentView.setRefreshComplete();
                }
                mCommentView.loadMoreFinish(emptyData, hasMore);
                mCommentView.setItems(articleComments);

                if (articleComments.size() == 0) {
                    mCommentView.showArticleCommentDialog();
                }
            }

            @Override
            public void onFailed(String message) {
                if (isRefreshing) {
                    mCommentView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                } else {
                    mCommentView.loadMoreError();
                }
            }
        });
    }

    @Override
    public void likeComment(final ArticleComment articleComment) {
        mArticleModel.likeComment(mCommentView.getPagerTag(), articleComment.getCommentId(), new IArticleModel.OnLoadResultListener() {
            @Override
            public void onSuccess(Object o, boolean emptyData, boolean hasMore) {
                // 点赞或者取消点赞
                boolean like = articleComment.getHasVote() == 1;
                articleComment.setHasVote(like ? 0 : 1);
                String voteCount = articleComment.getVoteCount();
                int count = 0;
                try {
                    count = Integer.parseInt(voteCount);
                } catch (NumberFormatException e) {
                    count = 0;
                } finally {
                    if (like) {
                        count--;
                    } else {
                        count++;
                    }

                    if (count < 0) {
                        count = 0;
                    }
                }

                articleComment.setVoteCount(NumberFormatUtils.getLargeFormatValue(count + ""));

                mCommentView.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String message) {

            }
        });
    }

    @Override
    public void submitComment(String text) {
        mArticleModel.submitComment(mCommentView.getPagerTag(), mArticleId, text, new IArticleModel.OnLoadResultListener() {
            @Override
            public void onSuccess(Object o, boolean emptyData, boolean hasMore) {
                loadComments(true);
            }

            @Override
            public void onFailed(String message) {

            }
        });
    }
}
